
*********************************************************
* Analysis Part 2: Tables 7-8
* 
* Topic: The Value of Ratings: Evidence from their Introduction in Securities Markets
* Authors: Asaf Bernstein, Carola Frydman, Eric Hilt
* 
* Code last updated: 5/23/25
* 
*************************************************************

*************************************************************
* I. Initial Code Settings/Directories
*************************************************************

clear all
* Set to folder of where do file is saved. Change if necessary.
cd "`=substr(c(current_do), 1, indexnot(c(current_do), "/", -1))'"

log using "Part2_BidAsk_Log.txt", text replace

*************************************************************
* II. Load in data & define variable
*************************************************************
	
use "spreads_instrument_bidask_data_panel.dta", clear

*************************************************************
* III. Analysis/output
*************************************************************

*************************************************************
* Table 7
*************************************************************

global file_name_new_v1 "T7"
global tbl_title "Table 7. IV Results: Effect of Ratings on Bid-Ask Spreads"

* Column 1

    reghdfe spr_wis post_rated trend_rated if post_othry!=. & trend_othry!=., absorb( date_std id_issue_bidask c.PreMean_YTM_Bid##i.date_std c.PreMean_Spread_pct##i.date_std) cluster(id_issue_bidask)
	est store t7c1

    *Num rated, unrated (same sample as regression) 
    preserve
	   keep if e(sample)==1
	   duplicates drop id_issue_bidask, force
	   tab rated_issue_manual
    restore
    *Share of obs rated	
     preserve
        keep if e(sample)==1
        tab rated_issue_manual
    restore

* Column 2

    ivreghdfe spr_wis (post_rated trend_rated = post_othry trend_othry) if  post_othry!=. & trend_othry!=., absorb( date_std id_issue_bidask c.PreMean_YTM_Bid##i.date_std c.PreMean_Spread_pct##i.date_std) cluster(id_issue_bidask) first
	est store t7c2

    *F-stat
    matrix first_stage = e(widstat)
	local first_f = first_stage[1,1]
	display "Kleibergen-Paap F, First Stage: " `first_f'
    *Num rated, unrated (same sample as regression) 
    preserve
	   keep if e(sample)==1
	   duplicates drop id_issue_bidask, force
	   tab rated_issue_manual
    restore
    *Share of obs rated	
    preserve
        keep if e(sample)==1
        tab rated_issue_manual
    restore
    *Weak IV confidence set
    twostepweakiv 2sls spr_wis (post_rated trend_rated = post_othry trend_othry) i.date_std i.id_issue_bidask c.PreMean_YTM_Bid##i.date_std c.PreMean_Spread_pct##i.date_std, cluster(id_issue_bidask)	project(post_rated)
 

* Column 3

    ivreghdfe spr_wis (post_rated trend_rated = post_othry trend_othry) if post_othry!=. & trend_othry!=. & ptiles_PreMean_Spread_pct>=60, absorb( date_std id_issue_bidask c.PreMean_YTM_Bid##i.date_std c.PreMean_Spread_pct##i.date_std) cluster(id_issue_bidask)
	est store t7c3

    *F-stat
    matrix first_stage = e(widstat)
	local first_f = first_stage[1,1]
	display "Kleibergen-Paap F, First Stage: " `first_f'
    *Num rated, unrated (same sample as regression) 
    preserve
	   keep if e(sample)==1
	   duplicates drop id_issue_bidask, force
	   tab rated_issue_manual
    restore
    *Share of obs rated	
    preserve
        keep if e(sample)==1
        tab rated_issue_manual
    restore
    *Weak IV confidence set
    twostepweakiv 2sls spr_wis (post_rated trend_rated = post_othry trend_othry) i.date_std i.id_issue_bidask c.PreMean_YTM_Bid##i.date_std c.PreMean_Spread_pct##i.date_std if ptiles_PreMean_Spread_pct>=60, cluster(id_issue_bidask)	project(post_rated)

	
* Column 4

    ivreghdfe spr_wis (post_rated trend_rated = post_othry trend_othry) if post_othry!=. & trend_othry!=. & ptiles_PreMean_Spread_pct>=80, absorb( date_std id_issue_bidask c.PreMean_YTM_Bid##i.date_std c.PreMean_Spread_pct##i.date_std) cluster(id_issue_bidask)
	est store t7c4

    *F-stat
    matrix first_stage = e(widstat)
	local first_f = first_stage[1,1]
	display "Kleibergen-Paap F, First Stage: " `first_f'
    *Num rated, unrated (same sample as regression) 
    preserve
	   keep if e(sample)==1
	   duplicates drop id_issue_bidask, force
	   tab rated_issue_manual
    restore
    *Share of obs rated	
    preserve
        keep if e(sample)==1
        tab rated_issue_manual
    restore
    *Weak IV confidence set
    twostepweakiv 2sls spr_wis (post_rated trend_rated = post_othry trend_othry) i.date_std i.id_issue_bidask c.PreMean_YTM_Bid##i.date_std c.PreMean_Spread_pct##i.date_std if ptiles_PreMean_Spread_pct>=80, cluster(id_issue_bidask)	project(post_rated)


*Table of results

    esttab t7c* using "$file_name_new_v1.rtf", replace compress nogaps se ///
	   drop(*cons*) ///
	   title($tbl_title) ///
	   addnote("") ///
	   label interaction(" X ") starlevels("{\super *}" 0.10 "{\super **}" 0.05 "{\super ***}" 0.01)

    tempname handle4
    rtfappend `handle4' using "$file_name_new_v1.rtf", replace
    file write `handle4' "\page"
    rtfclose `handle4'



*************************************************************
* Table 8
*************************************************************

global file_name_new_v1 "T8"
global tbl_title "Table 8. Reduced-Form Estimates"

* Column 1

    reghdfe spr_wis post_othry trend_othry if post_othry!=. & trend_othry!=., absorb( date_std id_issue_bidask c.PreMean_YTM_Bid##i.date_std c.PreMean_Spread_pct##i.date_std) cluster(id_issue_bidask)
	est store t8c1

    *F-stat from corresponding first stage
    quietly ivreghdfe spr_wis (post_rated trend_rated = post_othry trend_othry) if post_othry!=. & trend_othry!=., absorb( date_std id_issue_bidask c.PreMean_YTM_Bid##i.date_std c.PreMean_Spread_pct##i.date_std) cluster(id_issue_bidask) first
	   matrix first_stage = e(widstat)
	   local first_f = first_stage[1,1]
	   display "Kleibergen-Paap F, First Stage: " `first_f'
    *Num rated, unrated (same sample as regression) 
    preserve
	   keep if e(sample)==1
	   duplicates drop id_issue_bidask, force
	   tab rated_issue_manual
    restore
    *Share of obs rated
    preserve
        keep if e(sample)==1
        tab rated_issue_manual
    restore

* Column 2

    reghdfe spr_wis post_othry trend_othry if post_othry!=. & trend_othry!=.& ptiles_PreMean_Spread_pct>=60, absorb( date_std id_issue_bidask c.PreMean_YTM_Bid##i.date_std c.PreMean_Spread_pct##i.date_std) cluster(id_issue_bidask)
    est store t8c2
    
    *F-stat from corresponding first stage
    quietly ivreghdfe spr_wis (post_rated trend_rated = post_othry trend_othry) if post_othry!=. & trend_othry!=.& ptiles_PreMean_Spread_pct>=60, absorb( date_std id_issue_bidask c.PreMean_YTM_Bid##i.date_std c.PreMean_Spread_pct##i.date_std) cluster(id_issue_bidask) first
	   matrix first_stage = e(widstat)
	   local first_f = first_stage[1,1]
	   display "Kleibergen-Paap F, First Stage: " `first_f'
    *Num rated, unrated (same sample as regression)
    preserve
	   keep if e(sample)==1
	   duplicates drop id_issue_bidask, force
	   tab rated_issue_manual
    restore	
    *Share of obs rated
    preserve
        keep if e(sample)==1
        tab rated_issue_manual
    restore

* Column 3

    reghdfe spr_wis post_othry trend_othry if post_othry!=. & trend_othry!=.& ptiles_PreMean_Spread_pct>=80, absorb( date_std id_issue_bidask c.PreMean_YTM_Bid##i.date_std c.PreMean_Spread_pct##i.date_std) cluster(id_issue_bidask)
    est store t8c3
    
    *F-stat from corresponding first stage
    quietly ivreghdfe spr_wis (post_rated trend_rated = post_othry trend_othry) if post_othry!=. & trend_othry!=.& ptiles_PreMean_Spread_pct>=80, absorb( date_std id_issue_bidask c.PreMean_YTM_Bid##i.date_std c.PreMean_Spread_pct##i.date_std) cluster(id_issue_bidask) first
	   matrix first_stage = e(widstat)
	   local first_f = first_stage[1,1]
	   display "Kleibergen-Paap F, First Stage: " `first_f'
    *Num rated, unrated (same sample as regression)
    preserve
	   keep if e(sample)==1
	   duplicates drop id_issue_bidask, force
	   tab rated_issue_manual
    restore	
    *Share of obs rated
    preserve
        keep if e(sample)==1
        tab rated_issue_manual
    restore

* Column 4

    reghdfe spr_wis post_othry trend_othry if post_othry!=. & trend_othry!=.& ptiles_PreMean_Spread_pct<=20, absorb( date_std id_issue_bidask c.PreMean_YTM_Bid##i.date_std c.PreMean_Spread_pct##i.date_std) cluster(id_issue_bidask)
    est store t8c4
    
    *F-stat from corresponding first stage
    quietly ivreghdfe spr_wis (post_rated trend_rated = post_othry trend_othry) if post_othry!=. & trend_othry!=.& ptiles_PreMean_Spread_pct<=20, absorb( date_std id_issue_bidask c.PreMean_YTM_Bid##i.date_std c.PreMean_Spread_pct##i.date_std) cluster(id_issue_bidask) first
	   matrix first_stage = e(widstat)
	   local first_f = first_stage[1,1]
	   display "Kleibergen-Paap F, First Stage: " `first_f'
    *Num rated, unrated (same sample as regression)
    preserve
	   keep if e(sample)==1
	   duplicates drop id_issue_bidask, force
	   tab rated_issue_manual
    restore	
    *Share of obs rated
    preserve
        keep if e(sample)==1
        tab rated_issue_manual
    restore

* Column 5

    reghdfe spr_wis post_othry trend_othry if post_othry!=. & trend_othry!=.& ptiles_PreMean_Spread_pct<=60, absorb( date_std id_issue_bidask c.PreMean_YTM_Bid##i.date_std c.PreMean_Spread_pct##i.date_std) cluster(id_issue_bidask)
    est store t8c5
    
    *F-stat from corresponding first stage
    quietly ivreghdfe spr_wis (post_rated trend_rated = post_othry trend_othry) if post_othry!=. & trend_othry!=.& ptiles_PreMean_Spread_pct<=60, absorb( date_std id_issue_bidask c.PreMean_YTM_Bid##i.date_std c.PreMean_Spread_pct##i.date_std) cluster(id_issue_bidask) first
	   matrix first_stage = e(widstat)
	   local first_f = first_stage[1,1]
	   display "Kleibergen-Paap F, First Stage: " `first_f'
    *Num rated, unrated (same sample as regression)
    preserve
	   keep if e(sample)==1
	   duplicates drop id_issue_bidask, force
	   tab rated_issue_manual
    restore	
    *Share of obs rated
    preserve
       keep if e(sample)==1
       tab rated_issue_manual
    restore

*Table of results

    esttab t8c* using "$file_name_new_v1.rtf", replace compress nogaps se ///
	   drop(*cons*) ///
	   title($tbl_title) ///
	   addnote("") ///
	   label interaction(" X ") starlevels("{\super *}" 0.10 "{\super **}" 0.05 "{\super ***}" 0.01)

    tempname handle4
    rtfappend `handle4' using "$file_name_new_v1.rtf", replace
    file write `handle4' "\page"
    rtfclose `handle4'


log close
